# 练习

# 1,把字典中的键值对转换为 a=b 的形式
# {'user': 'Jack', 'age': 20, 'phone': '123321'} ===> ['user=Jack', 'age=20', 'phone=123321']
vard = {'user': 'Jack', 'age': 20, 'phone': '123321'}
varl = []
for i in vard:
    varl.append(f'{i}={vard[i]}')
print(varl)
#     ['user=Jack', 'age=20', 'phone=123321']

varl = [f'{i}={vard[i]}' for i in vard]
print(varl)
# ['user=Jack', 'age=20', 'phone=123321']

# 转换为字符串
vars = ''
for i in vard:
    vars = f'{vars}&{i}={vard[i]}'
vars = vars[1:]
print(vars)
# user=Jack&age=20&phone=123321

# 2,列表推导式完成转换为小写
# ['AAAAA', 'BBbBb', 'ccCCc'] ===> ['aaaaa', 'bbbbb', 'ccccc']
varl = ['AAAAA', 'BBbBb', 'ccCCc']
newlist = [i.lower() for i in varl]
print(newlist)
# ['aaaaa', 'bbbbb', 'ccccc']


# 3, x时0-5之间的偶数，y是0-5之间的奇数，把x,y组成一个元组，放到列表中
newlist = [(i, j) for i in range(0, 6) for j in range(0, 6) if i % 2 == 0 and j % 2 == 1]
print(newlist)
# [(0, 1), (0, 3), (0, 5), (2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]

# 4, 求m,n中矩阵和元素的乘积
'''
M = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
N = [
    [2, 2, 2],
    [3, 3, 3],
    [4, 4, 4]
]

(1) ===> [2, 4, 6, 12, 15, 18, 28, 32, 36]
(2) ===> [[2, 4, 6], [12, 15, 18], [28, 32, 36]]
'''
M = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
N = [
    [2, 2, 2],
    [3, 3, 3],
    [4, 4, 4]
]
newlist = [M[j][i]*N[j][i] for i in range(0, len(M[0])) for j in range(0, len(M))]
print(newlist)
# [2, 4, 6, 12, 15, 18, 28, 32, 36]
newlist = [[M[i][j]*N[i][j] for j in range(0, len(M[0]))] for i in range(0, len(M))]
print(newlist)
# [[2, 4, 6], [12, 15, 18], [28, 32, 36]]

